#include "aip-cpp-sdk/face.h"
#include "interface.h"
#include <vector>

using namespace std;
Face::Face(string &model)
{
    classifier.load(model);

}

//人脸检测
void Face::detect(cv::Mat image)
{
       Rect face(0,0,0,0);
        //3通道转化为灰度图
       cvtColor(image,image,CV_BGR2GRAY);
       //均衡化处理
       equalizeHist(image,image);
       vector<Rect>faces;
       classifier.detectMultiScale(image,faces,1.1,3,0,Size(50,50));
       if(faces.size())
       {
           face = faces[0];
           emit detected(face);

       }

}

//人脸识别
void Face::recongnize(Mat face)
{
    cout<<"人脸识别中"<<endl;
    Employee e;

    string app_id = "24809667";
    string api_key = "BlDLZ3me974w8mXUBLchcI4C";
    string sercet_key = "1dqmXPYn1fsMQwa7i3H1Y9AYZlDWpGZQ";
    aip::Face client(app_id,api_key,sercet_key);

    vector<unsigned char>buf;
    imencode(".jpg",face,buf);
    string faceimg=aip::base64_encode((char *)buf.data(),buf.size());//转为base64编码的字符串
    Json::Value json = client.search(faceimg,"BASE64","group1",aip::null);

    if(json["error_code"].asInt())
    {
        cout<< json["error_msg"] <<endl;
    }
    cout<<json <<endl;
    e.user_id = json["result"]["user_list"][0]["user_id"].asString();
    e.score = json["result"]["user_list"][0]["score"].asDouble();
    if(e.score<50)
    {
        cout<<"人脸识别失败"<<endl;
    }
    else {

        cout<<"人脸识别成功"<<endl;
    }
    emit recongnized(e);


}
